package com.lb.algorithm.stack;

import java.util.Stack;

/**
 * @version 1.0
 * @Author: Edward
 * @Date: 2021/10/25 17:56
 * @Description:
 */
public class MyStack1 {
    private Stack<Integer> stackData;
    private Stack<Integer> minStack;

    public MyStack1() {
        this.stackData = new Stack<>();
        this.minStack = new Stack<>();
    }

    /**
     * push方法
     * @param newNum
     */
    public void push(int newNum) {
        if (this.minStack.isEmpty()) {
            this.minStack.push(newNum);
        } else if (newNum <= this.getMin()) {
            this.minStack.push(newNum);
        }
        this.stackData.push(newNum);
    }

    /**
     * pop最小值
     * @return
     */
    public int pop() {
        if (this.stackData.isEmpty()) {
            throw new RuntimeException("Your stack is empty!");
        }
        int value = this.stackData.pop();
        if (value == this.getMin()) {
            this.minStack.pop();
        }
        return value;
    }

    /**
     * 最小值
     */
    public int getMin() {
        if (this.minStack.isEmpty()) {
            throw new RuntimeException("minStack is empty!");
        }
        return this.minStack.peek();
    }

    public static void main(String[] args) {
        MyStack1 myStack1 = new MyStack1();
        myStack1.push(1);
        myStack1.push(2);
        myStack1.push(0);
        myStack1.push(4);
        System.out.println(myStack1.pop());
        System.out.println(myStack1.getMin());
    }
}
